package com.user;

import com.utils.JdbcUtils;
import com.utils.JsonUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet(name="QueryUser",urlPatterns={"/QueryUser"})
public class QueryUser extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String sql = "select * from animalfile09_systemuser where UserID = ?";
        Connection con = JdbcUtils.getCon();
        PreparedStatement stat = null;
        ResultSet rs = null;
        try {
            stat = con.prepareStatement(sql);
            stat.setString(1, id);
            rs = stat.executeQuery();
            if (rs.next()) {
                SysUser sysUser = new SysUser();
                sysUser.setUserId(rs.getString("UserID"));
                sysUser.setName(rs.getString("Name"));
                sysUser.setUserGender(rs.getString("UserGender"));
                sysUser.setUserPhone(rs.getString("UserPhone"));
                sysUser.setUserEmail(rs.getString("UserEmail"));
                sysUser.setUserPosition(rs.getString("UserPosition"));
                sysUser.setUserSystemPermission(rs.getString("UserSystemPermission"));
                String json = JsonUtil.obj2Json(sysUser);
                System.out.println(sysUser);
                System.out.println(json);
                resp.setContentType("application/json;charset=utf-8");
                resp.getWriter().println(json);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JdbcUtils.close3(rs, con, stat);
        }
    }
}
